//
//  MessageTableViewCell.m
//  EducationAdminClient
//
//  Created by kwsdzjx on 16/7/29.
//  Copyright © 2016年 周佳兴. All rights reserved.
//

#import "MessageTableViewCell.h"
#import "Masonry.h"
#import "UIColor+CL.h"
#import "KeyValuesClass.h"
@implementation MessageTableViewCell

-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    
    if (self) {
        UIView *view = [self createUI];
        [self.contentView addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(14/2);
            make.top.equalTo(self.contentView.mas_top).offset(14/2);
            make.bottom.equalTo(self.contentView.mas_bottom);
            make.right.equalTo(self.contentView.mas_right).offset(-14/2);
        }];
    }
    
    return self;
}

-(UIView *)createUI{
    UIView *baseView = [[UIView alloc]init];
    
    _labelName = [[UILabel alloc]init];
    [baseView addSubview:_labelName];
    
    _badgeMessage = [[UILabel alloc] init];
    [baseView addSubview:_badgeMessage];
    
    _labelMessage = [[UILabel alloc]init];
    [baseView addSubview:_labelMessage];
    
    _labelMessageDate = [[UILabel alloc]init];
    [baseView addSubview:_labelMessageDate];
    
    _labelMessageState = [[UILabel alloc]init];
    [baseView addSubview:_labelMessageState];
    
    _labelMessageDetail = [[UILabel alloc]init];
    [baseView addSubview:_labelMessageDetail];
    
    
    [_labelMessage mas_makeConstraints:^(MASConstraintMaker *make) {//标题
        make.left.equalTo(baseView.mas_left).offset(18/2);
        make.top.equalTo(baseView.mas_top).offset(28/2);
        make.width.equalTo(@(488/2));
        make.height.equalTo(@(32/2));
    }];
    
    [_badgeMessage mas_makeConstraints:^(MASConstraintMaker *make) {//消息状态
//        make.left.equalTo(_labelMessage.mas_right);
        make.right.equalTo(baseView.mas_right).offset(-18/2);
        make.width.equalTo(@(28));
        make.height.equalTo(@(15));
        make.centerY.equalTo(_labelMessage.mas_centerY);
    }];
    _badgeMessage.layer.masksToBounds = YES;
    _badgeMessage.layer.cornerRadius = 7.0f;
//    _badgeMessage.backgroundColor = [UIColor redColor];
    _badgeMessage.text = @"";
    _badgeMessage.textColor = [UIColor whiteColor];
    _badgeMessage.font = [UIFont systemFontOfSize:24/2];
    _badgeMessage.textAlignment = NSTextAlignmentCenter;
    [_labelName mas_makeConstraints:^(MASConstraintMaker *make) {//销售经理01
        make.left.equalTo(_labelMessage.mas_left);
        make.top.equalTo(_labelMessage.mas_bottom).offset(36/2);
        
        //        make.right.equalTo(_labelMessageState.mas_left);
                make.height.equalTo(@(30/2));
    }];
    
    
    [ _labelMessageDetail mas_makeConstraints:^(MASConstraintMaker *make) {//您好，有一个学员：。。。。。
        make.left.equalTo(_labelName.mas_left);
        make.top.equalTo(_labelName.mas_bottom).offset(44/2);
        make.right.equalTo(baseView.mas_right).offset(-24/2);
        //        make.height.equalTo(@(30));
    }];
    
    
    
    UIImageView *lineImg = [[UIImageView alloc]init];
    [baseView addSubview:lineImg];
    [lineImg mas_makeConstraints:^(MASConstraintMaker *make) {//分割线
        make.left.equalTo(_labelMessageDetail.mas_left);
        make.top.equalTo(_labelMessageDetail.mas_bottom).offset(44/2);
        make.height.equalTo(@(1));
        make.right.equalTo(_labelMessageDetail.mas_right).offset(20-18/2);
    }];
    
    [_labelMessageState mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineImg.mas_bottom).offset(18/2);
        //        make.right.equalTo(baseView.mas_right).offset(-5);
        //        make.height.equalTo(@(30));
        make.left.equalTo(lineImg.mas_left);
    }];
    
    
    [_labelMessageDate mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(baseView.mas_right).offset(-18/2);
        make.centerY.equalTo(_labelMessageState.mas_centerY);
        //        make.right.equalTo(_labelMessageDate.mas_right);
        //        make.height.equalTo(@(30));
    }];
    
    _labelMessage.font = [UIFont systemFontOfSize:34/2];
    _labelMessage.textColor = [UIColor colorWithHex:0x333333];
    
    
    _labelName.font = [UIFont systemFontOfSize:30/2];
    _labelName.textColor = [UIColor colorWithHex:0x5b5b5b];
    
    _labelMessageDetail.font = [UIFont systemFontOfSize:30/2];
    _labelMessageDetail.textColor = [UIColor colorWithHex:0x858585];
    
    
    
    //    lineImg.backgroundColor = [UIColor colorWithHex:0xd3d3d3];
    lineImg.image = [UIImage imageNamed:@"line"];
    
    _labelMessageState.font = [UIFont systemFontOfSize:24/2];
    _labelMessageState.textColor = [UIColor colorWithHex:0x858585];
    
    
    _labelMessageDate.font = [UIFont systemFontOfSize:24/2];
    _labelMessageDate.textColor = [UIColor colorWithHex:0x858585];
    
    _labelMessageDetail.numberOfLines = 2;
    _labelMessageDetail.lineBreakMode = NSLineBreakByTruncatingTail;
    
    baseView.layer.cornerRadius = 8;
    
    baseView.backgroundColor = [UIColor whiteColor];
    return baseView;
}

-(void)setModel:(MessageModel *)model{
    _model = model;
    
    /*
     +(NSString *)dateGlinToFormate:(NSString *)date andFromate:(NSString *)formate{
     NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
     if(!formate){
     
     [formatter setDateFormat:@"yyyy-MM-dd HH:mm:SS"];
     }else{
     [formatter setDateFormat:formate];
     }
     NSString *dateLoca = [NSString stringWithFormat:@"%@",date];
     NSTimeInterval time=[dateLoca doubleValue]/1000.0;
     NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
     NSDate *dateBJTime = [[NSDate alloc]initWithTimeInterval:8*60*60 sinceDate:detaildate];
     
     
     NSString *destDateString = [formatter stringFromDate:dateBJTime];
     
     
     return destDateString;
     }
     
     */
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US"];//_POSIX
    
    [formatter setLocale:enUSPOSIXLocale];
    
    
    NSString *myTestStr = [self userVisibleDateTimeStringForRFC3339DateTimeString2:model.create_date];
    NSLog(@"%@",myTestStr);
    NSLog(@"%@",model.create_date);
    NSString *dateLoca = [NSString stringWithFormat:@"%@",model.create_date];
    NSTimeInterval time=[dateLoca doubleValue]/1000.0;
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    NSDate *dateBJTime = [[NSDate alloc]initWithTimeInterval:0 sinceDate:detaildate];
    
    NSString *string = [KeyValuesClass dateGlinToFormate:model.create_date andFromate:nil]; // 由于时间格式返回的都是  格林尼治 所以
    NSLog(@"%@",string);
    NSString *destDateString = [formatter stringFromDate:dateBJTime];
    
    _labelName.text = [NSString stringWithFormat:@"%@", model.notice_name];
    _labelMessage.text = [NSString stringWithFormat:@"%@", model.title];
    _labelMessageDate.text = [NSString stringWithFormat:@"%@", destDateString];
    _labelMessageDetail.text = [NSString stringWithFormat:@"%@", model.content];
    _labelMessageState.text = [NSString stringWithFormat:@"阅读状态:%@", model.if_seeCn];
//    NSMutableAttributedString *string =[[NSMutableAttributedString alloc]initWithString:_labelMessageDetail.text];
//    CGFloat lineSpace = 100;
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:_labelMessageDetail.text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    
    [paragraphStyle setLineSpacing:12/2];//调整行间距
    
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [_labelMessageDetail.text length])];
//    _labelMessageDetail.text = @"";
    _labelMessageDetail.attributedText = attributedString;
//    [contentView addSubview:_labelMessageDetail];
    [_labelMessageDetail sizeToFit];

}
static NSDateFormatter *    sUserVisibleDateFormatter;



- (NSString *)userVisibleDateTimeStringForRFC3339DateTimeString2:(NSString *)rfc3339DateTimeString

// Returns a user-visible date time string that corresponds to the

// specified RFC 3339 date time string. Note that this does not handle

// all possible RFC 3339 date time strings, just one of the most common

// styles.

{
    
    static NSDateFormatter *    sRFC3339DateFormatter;
    
    NSString *                  userVisibleDateTimeString;
    
    NSDate *                    date;
    
    
    
    // If the date formatters aren't already set up, do that now and cache them
    
    // for subsequence reuse.
    
    
    
    if (sRFC3339DateFormatter == nil) {
        
        NSLocale *                  enUSPOSIXLocale;
        
        
        
        sRFC3339DateFormatter = [[NSDateFormatter alloc] init];
        
        
        
        enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
        
        
        
        [sRFC3339DateFormatter setLocale:enUSPOSIXLocale];
        
        [sRFC3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
        
        [sRFC3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
        
    }
    
    
    
    if (sUserVisibleDateFormatter == nil) {
        
        sUserVisibleDateFormatter = [[NSDateFormatter alloc] init];
        
        
        
        [sUserVisibleDateFormatter setDateStyle:NSDateFormatterShortStyle];
        
        [sUserVisibleDateFormatter setTimeStyle:NSDateFormatterShortStyle];
        
    }
    
    
    
    // Convert the RFC 3339 date time string to an NSDate.
    
    // Then convert the NSDate to a user-visible date string.
    
    
    
    userVisibleDateTimeString = nil;
    
    
    
    date = [sRFC3339DateFormatter dateFromString:rfc3339DateTimeString];
    
    if (date != nil) {
        
        userVisibleDateTimeString = [sUserVisibleDateFormatter stringFromDate:date];
        
    }
    
    return userVisibleDateTimeString;
    
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
